package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.foxcard.UserFoxCardData;
import com.yanqu.road.entity.activity.yuanbaolottery.UserYuanBaoLotteryData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserYuanBaoLotteryDaoImpl extends TempDataObjectDao<UserYuanBaoLotteryData> {

    public static String getUserYuanBaoLotteryDataSql = "select * from `t_u_yuanbao_lottery` where `activityId`=? and `userId`=?";

    public UserYuanBaoLotteryData getUserYuanBaoLotteryData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserYuanBaoLotteryDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserYuanBaoLotteryDataSql = "update `t_u_yuanbao_lottery` set round=?,lotteryTimes=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserYuanBaoLotteryData userYuanBaoLotteryData) {
        userYuanBaoLotteryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userYuanBaoLotteryData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userYuanBaoLotteryData.getLotteryTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userYuanBaoLotteryData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userYuanBaoLotteryData.getUserId()));
        if (getDb().executeNoneQuery(updateUserYuanBaoLotteryDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserYuanBaoLotteryDataSql = "insert into `t_u_yuanbao_lottery`(activityId,userId,round,lotteryTimes) values (?,?,?,?)";

    @Override
    protected boolean addData(UserYuanBaoLotteryData userYuanBaoLotteryData) {
        userYuanBaoLotteryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userYuanBaoLotteryData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userYuanBaoLotteryData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userYuanBaoLotteryData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userYuanBaoLotteryData.getLotteryTimes()));
        if (getDb().executeNoneQuery(insertUserYuanBaoLotteryDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserYuanBaoLotteryData parseData(ResultSet rs) throws SQLException {
        UserYuanBaoLotteryData userYuanBaoLotteryData = new UserYuanBaoLotteryData();
        userYuanBaoLotteryData.setActivityId(rs.getInt("activityId"));
        userYuanBaoLotteryData.setUserId(rs.getLong("userId"));
        userYuanBaoLotteryData.setRound(rs.getInt("round"));
        userYuanBaoLotteryData.setLotteryTimes(rs.getInt("lotteryTimes"));

        return userYuanBaoLotteryData;
    }
}
